package org.sharelife.action;

import java.io.File;

import org.apache.struts2.ServletActionContext;
import org.sharelife.commons.Constants;
import org.sharelife.entity.Picture;
import org.sharelife.entity.User;
import org.sharelife.service.PictureService;
import org.sharelife.util.PageBean;
import org.sharelife.util.UploadUtils;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class PictureAction extends ActionSupport {

	private File up;
	
	private String upFileName;
	
	private Picture picture;
	
	private PictureService pictureService;
	
	private User user;
	
	private Constants constants;
	
	private String album;
	
	private int page = 1;
	
	private int pageSize = 8;
	
	private int showSize = 20;
	
	private int size = 25;
	
	private PageBean pageBean;
	
	private int type;
	
	private String keyWord;
	
	/**
	 * @Description 文件上传
	 * @return
	 * @throws Exception
	 */
	public String upload() throws Exception {
		try {
			picture.setPicType(UploadUtils.formatPicType(type));
			System.out.println("picType == " + picture.getPicType());
			upFileName = UploadUtils.formatFileName(upFileName);
			UploadUtils.upload(up, upFileName, album);
			picture.setPicName(upFileName);
			picture.setUrl(UploadUtils.photoURL(upFileName, album));
			if (pictureService.add(picture)) {
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "input";
	}
	
	/**
	 * @Description 头像上传
	 * @return
	 * @throws Exception
	 */
	public String uploadPhoto() throws Exception {
		try {
			upFileName = UploadUtils.formatFileName(upFileName);
			UploadUtils.upload(up, upFileName, user.getAlbum());
			UploadUtils.scale(upFileName, user.getAlbum());
			upFileName = UploadUtils.photoURL(upFileName, user.getAlbum());
			ServletActionContext.getResponse().getWriter().write(upFileName);
			return NONE;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "input";
	}
	
	/**
	 * @Description 分页查看个人图片
	 * @return
	 * @throws Exception
	 */
	public String findForPage() throws Exception {
		try {
			pageBean = pictureService.findForPage(picture.getUser().getId(), pageSize, page);
			System.out.println("picture.user.id == " + picture.getUser().getId());
			System.out.println("pageBean.list.size == " + pageBean.getList().size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "find_personal_success";
	}
	
	/**
	 * @Description 前台查看所有
	 * @return
	 * @throws Exception
	 */
	public String findAll() throws Exception {
		try {
			pageBean = pictureService.findAll(showSize, page);
			System.out.println("pageBean.list.size == " + pageBean.getList().size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "find_all_success";
	}
	
	/**
	 * @Description 根据ID查看
	 * @return
	 * @throws Exception
	 */
	public String findById() throws Exception {
		try {
			picture = pictureService.findById(picture.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "find_id_success";
	}
	
	/**
	 * @Description 根据图片类别查看
	 * @return
	 * @throws Exception
	 */
	public String findByType() throws Exception {
		keyWord = UploadUtils.formatPicType(type);
		try {
			pageBean = pictureService.queryByType(keyWord, showSize, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("pageBean.list.size == " + pageBean.getList().size());
		return "findByType";
	}
	
	// 后台查看所有
	public String findInfoAll() throws Exception {
		try {
			pageBean = pictureService.queryForPage(size, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "find_info_all_success";
	}
	//后台删除
	public String delete()throws Exception{
		try {
			pictureService.delete(picture.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete_success";
	}
	
	/**
	 * @Description 根据关键字查看
	 * @return
	 * @throws Exception
	 */
	public String findByKeyWord() throws Exception {
		try {
			pageBean = pictureService.queryByKeyWord(keyWord, showSize, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "findByKeyWord";
	}
	
	public File getUp() {
		return up;
	}

	public void setUp(File up) {
		this.up = up;
	}

	public String getUpFileName() {
		return upFileName;
	}

	public void setUpFileName(String upFileName) {
		this.upFileName = upFileName;
	}

	public Picture getPicture() {
		return picture;
	}

	public void setPicture(Picture picture) {
		this.picture = picture;
	}

	public PictureService getPictureService() {
		return pictureService;
	}

	public void setPictureService(PictureService pictureService) {
		this.pictureService = pictureService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Constants getConstants() {
		return constants;
	}

	public void setConstants(Constants constants) {
		this.constants = constants;
	}

	public String getAlbum() {
		return album;
	}

	public void setAlbum(String album) {
		this.album = album;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getShowSize() {
		return showSize;
	}

	public void setShowSize(int showSize) {
		this.showSize = showSize;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
}